﻿using Abp.Application.Services.Dto;
using Abp.Web.Mvc.Authorization;
using JHT.ABPLearn.Tasks;
using JHT.ABPLearn.Tasks.Dto;
using JHT.ABPLearn.Users;
using JHT.ABPLearn.Web.Models.Tasks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace JHT.ABPLearn.Web.Controllers
{
    [AbpMvcAuthorize]
    public class AbpTasksController : ABPLearnControllerBase
    {
        private readonly IAbpTaskAppService abpTaskAppService;
        private readonly IUserAppService userAppService;

        public AbpTasksController(IAbpTaskAppService abpTaskAppService, IUserAppService userAppService)
        {
            this.abpTaskAppService = abpTaskAppService;
            this.userAppService = userAppService;
        }



        [ChildActionOnly]
        public PartialViewResult Create()
        {
            var users = userAppService.GetAll(); //Paging not implemented yet
            ViewBag.AssignedPersonId = new SelectList(users.Items, "Id", "Name");
            return PartialView("CreateTask");
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(CreateTaskInput task)
        {
            var id = abpTaskAppService.CreateTask(task);
            var input = new GetTasksInput();
            var output = abpTaskAppService.GetTasks(input);
            return PartialView("ListView", output.Tasks);
        }

        public ActionResult Index(GetTasksInput input)
        {
            var output = abpTaskAppService.GetTasks(input);

            var model = new IndexViewModel(output.Tasks)
            {
                SelectedTaskState = input.State
            };
            return View(model);
        }

    }
}